#include "Configuration.hh"
#include "nlohmann/json.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using std::string;
using std::ifstream;
using std::vector;
using std::pair;
using std::make_pair;
using std::cout;
using std::endl;

Configuration* Configuration::_instance = nullptr;

Configuration::Configuration(const string& cong_path){
    // 读配置文件

    nlohmann::json j;
    ifstream cong_file(cong_path);
    cong_file >> j;
    
    string stop_words_eng_path = j["stop_words_eng_path"];
    string stop_words_zh_path = j["stop_words_zh_path"];

    string dict_path = j["dict_path"];
    string model_path = j["model_path"];
    string user_dict_path = j["user_dict_path"];
    string idfPath = j["idfPath"];
    string stopWordPath = j["stopWordPath"];
    string dict_path_en = j["dict_path_en"];
    string dict_path_zh = j["dict_path_zh"];
    string dict_index_path = j["dict_index_path"];

    vector<string> art;
    for(auto& item : j["art"]){
        art.push_back(item);
    }

    vector<string> art_en;
    for(auto& item : j["art_en"]){
        art_en.push_back(item);
    }


    string ripepage = j["ripepage"];
    string offset = j["offset"];
    string newripepage = j["newripepage"];
    string newoffset = j["newoffset"];

    vector<string> yuliao;
    for(auto& item : j["yuliao"]){
        yuliao.push_back(item);
    }
    

    _config.insert({"stop_words_eng_path", {stop_words_eng_path}});
    _config.insert({"stop_words_zh_path", {stop_words_zh_path}});
    _config.insert({"dict_path", {dict_path}});
    _config.insert({"model_path", {model_path}});
    _config.insert({"user_dict_path", {user_dict_path}});
    _config.insert({"idfPath", {idfPath}});
    _config.insert({"stopWordPath", {stopWordPath}});
    _config.insert({"art", art});
    _config.insert({"art_en", art_en});
    _config.insert({"yuliao", yuliao});
    _config.insert({"dict_path_en", {dict_path_en}});
    _config.insert({"dict_path_zh", {dict_path_zh}});
    _config.insert({"dict_index_path", {dict_index_path}});
    _config.insert({"ripepage", {ripepage}});
    _config.insert({"offset", {offset}});
    _config.insert({"newripepage", {newripepage}});
    _config.insert({"newoffset", {newoffset}});


    string invertIndex = j["invertIndex"];
    _config.insert({"invertIndex", {invertIndex}});

    string dictIndex = j["dictIndex"];
    _config.insert({"dictIndex", {dictIndex}});

    string ip = j["ip"];
    _config.insert({"ip", {ip}});

    
    string port = j["port"];
    _config.insert({"port", {port}});
    

    string redis = j["redis"];
    _config.insert({"redis", {redis}});


    string cacahe_file = j["cacahe_file"];
    _config.insert({"cacahe_file", {cacahe_file}});


    string level = j["level"];
    _config.insert({"level", {level}});


    string logFile = j["logFile"];
    _config.insert({"logFile", {logFile}});

    string logRoll = j["logRoll"];
    _config.insert({"logRoll", {logRoll}});

    // NOTE: 未完待续

    ifstream ifs;

    // 设置英文停用词
    ifs.open(stop_words_eng_path);
    string word;
    while(ifs >> word){
        _stopWordEn.insert(word);
    }
    ifs.close();
    
    // 设置中文停用词
    ifs.open(stop_words_zh_path);
    while(ifs >> word){
        _stopWordZh.insert(word);
    }
    ifs.close();
}



Configuration* Configuration::createConfig(){
    if(_instance == nullptr){
        _instance = new Configuration();
    }
    return _instance;
}



void Configuration::destoryConfig(){
    if(_instance != nullptr){
        delete _instance;
        _instance = nullptr;
    }
}